// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
// Modify by Fernando Cerqueira To DevPhone Toolkit

using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

// ReSharper disable CheckNamespace
namespace DevPhone.Controls
// ReSharper restore CheckNamespace
{
    public class TimePickerBox : DateTimePickerBase
    {
        private Image _iconImage;

        /// <summary>
        /// Initializes a new instance of the DatePickerBox control.
        /// </summary>
        public TimePickerBox()
        {
            DefaultStyleKey = typeof(TimePickerBox);
            ValueStringFormat = "{0:t}";
            Value = DateTime.Now;
            ShowMode = DateTimePickerMode.TimePickerMode;
        }

        public static readonly DependencyProperty ShowIconProperty =
            DependencyProperty.Register("ShowIcon", typeof(bool), typeof(TimePickerBox), new PropertyMetadata(false, ShowIconChangedCallback));

        private static void ShowIconChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = (TimePickerBox)d;
            if (instance._iconImage != null)
            {
                if ((bool)e.NewValue)
                {
                    instance._iconImage.Source = instance.ExternalIcon ?? AcquireIcon();
                    instance._iconImage.Visibility = Visibility.Visible;
                }
                else
                {
                    instance._iconImage.Visibility = Visibility.Collapsed;
                }
            }
        }

        public bool ShowIcon
        {
            get { return (bool)GetValue(ShowIconProperty); }
            set { SetValue(ShowIconProperty, value); }
        }

        public static readonly DependencyProperty ExternalIconSourceProperty =
            DependencyProperty.Register("ExternalIcon", typeof(ImageSource), typeof(TimePickerBox), new PropertyMetadata(null, ExternalIconChangedCallback));

        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(object), typeof(TimePickerBox), new PropertyMetadata(null));

        public object Header
        {
            get { return GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        public static readonly DependencyProperty HeaderTemplateProperty =
            DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(TimePickerBox), new PropertyMetadata(null));

        public DataTemplate HeaderTemplate
        {
            get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
            set { SetValue(HeaderTemplateProperty, value); }
        }

        private static void ExternalIconChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = (TimePickerBox)d;
            if (instance._iconImage != null)
            {
                if (instance.ShowIcon)
                {
                    instance._iconImage.Source = (ImageSource)e.NewValue ?? AcquireIcon();
                }
            }
        }

        public ImageSource ExternalIcon
        {
            get { return (ImageSource)GetValue(ExternalIconSourceProperty); }
            set { SetValue(ExternalIconSourceProperty, value); }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _iconImage = GetTemplateChild("IconImage") as Image;
            if (_iconImage != null)
            {
                if (ShowIcon)
                {
                    _iconImage.Source = ExternalIcon ?? AcquireIcon();
                    _iconImage.Visibility = Visibility.Visible;
                }
                else
                {
                    _iconImage.Visibility = Visibility.Collapsed;
                }
            } 
            UpdateValueString();
        }

        private static BitmapImage AcquireIcon()
        {
            var result = new BitmapImage();
            using (var resourceStream =
                Assembly.GetExecutingAssembly().GetManifestResourceStream("DevPhone.Controls.DateTimePickers.Icon_TimePicker.png"))
            {
                //Verify if the resource is valid
                if (resourceStream != null)
                {
                    result.SetSource(resourceStream);

                }
            }
            return result;
        }

    }
}